package com.kun.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果封装
 *
 * @author kun.li
 */
@Data
@AllArgsConstructor
public final class ResponseResult<T> implements Serializable {
    /**
     * 响应状态
     */
    private Boolean state;

    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应结果
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    private ResponseResult() {

    }

    public static <T> ResponseResult<T> failed() {
        return failed("处理失败");
    }

    public static <T> ResponseResult<T> failed(String msg) {
        return failed(500, msg);
    }


    public static <T> ResponseResult<T> failed(Integer code, String msg) {
        return of(false, code, msg, null);
    }

    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return success("处理成功", data);
    }

    public static <T> ResponseResult<T> success(String msg, T data) {
        return of(true, 200, msg, data);
    }


    public static <T> ResponseResult<T> of(Boolean state, Integer code, String msg, T data) {
        return new ResponseResult<>(state, code, msg, data);
    }

}
